//
//  BYTextField.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/2/13.
//

#import "BYTextField.h"

@implementation BYTextField

- (void)dealloc
{
    [kNotificationCenterSington removeObserver:self];
}

- (instancetype)init{
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.isCanContinueEdit = YES;
        
        //注意，这里的通知监听方法中的最后一个参数object，一定要传入当前JZGTextField对象，才会监听对应的JZGTextField，否则会监听所有JZGTextField
        [kNotificationCenterSington addObserver:self
                                       selector:@selector(textFieldDidBeginEditingNotifiction:)
                                           name:UITextFieldTextDidBeginEditingNotification
                                         object:self];
        
        [kNotificationCenterSington addObserver:self
                                       selector:@selector(textFieldChanged:)
                                           name:UITextFieldTextDidChangeNotification
                                         object:self];
    }
    return self;
}


#pragma mark - Notification Methods
- (void)textFieldDidBeginEditingNotifiction:(NSNotification *)f{
    self.isCanContinueEdit = YES;
}

- (void)textFieldChanged:(NSNotification *)noti {
//    NSLog(@"1-->>>self.text==%@,_limitTextLength==%@\n\n",self.text,@(_limitTextLength));

    if (self.limitTextLength<=0) {
        self.isCanContinueEdit = YES;
        
        if (self.textFieldChangeBlock) {
            self.textFieldChangeBlock(YES);
        }
        return;
    }

    //获取当前键盘类型
    UITextInputMode *mode = (UITextInputMode *)[UITextInputMode activeInputModes][0];
    //获取当前键盘语言
    NSString *lang = mode.primaryLanguage;
    //如果语言是汉语(拼音)
    if ([lang isEqualToString:@"zh-Hans"]
        || [lang isEqualToString:@"en-US"]
        || [lang isEqualToString:@"emoji"])
    {
        //取到高亮部分范围
        UITextRange *selectedRange = [self markedTextRange];
        //取到高亮部分
        UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
        
        //如果取不到高亮部分,代表没有拼音
        if (!position){
//            NSLog(@"2-->>>self.text==%@,self.text.length==%@",self.text,@(self.text.length));
            
            //处理文本信息，超过限定长度后，直接裁剪到限定长度
            self.isCanContinueEdit = [self handleText];
        }else{
            //表示还有高亮部分，暂不处理
        }
    }else{//如果语言不是汉语,直接计算
        //处理文本信息，超过限定长度后，直接裁剪到限定长度
        self.isCanContinueEdit = [self handleText];
    }
    if (self.textFieldChangeBlock) {
        self.textFieldChangeBlock(self.isCanContinueEdit);
    }
}

#pragma mark - Private Methods
//处理文本信息，超过限定长度后，直接裁剪到限定长度
- (BOOL)handleText {
    //当期超过最大限制时
    if (self.text.length > self.limitTextLength) {
        //对超出部分进行裁剪
        self.text = [self.text substringToIndex:self.limitTextLength];
        self.canInputLength = 0;
        //同时对可继续书写属性设为否,shouldChangeTextInRange方法会调用
        return  NO;
    } else {
        self.canInputLength = self.limitTextLength - self.text.length;
        return YES;
    }
}

- (BOOL)canContinueEditWithText:(NSString *)str alertMsg:(NSString *)msg {
    if ([str isEqualToString:@""]) {
        return YES;
    } else {
        if (self.isCanContinueEdit == NO) {
            if (ValidStr(msg)) {
                [kKeyWindow makeToast:msg];
            }
        }
        return self.isCanContinueEdit;
    }
}

@end
